$(function(){
	page.bind();
	page.getMaterialCode();
	page.getMaterialName();
	page.getModel();
	page.getUnit();
	page.getDefaultWarehouse();
	page.getProviderName();
	page.getMaterialType();
	page.getColor();
	queryMaterial();
});

var page = {
	bind:function(){
		//调用模态框show方法后触发
		$('#addMaterialModal').on('show.bs.modal', function () {
			//加载物料名称
			$.ajax({
				url:"basicData/getMaterialName",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#materialNameList").html("");
						$("#materialNameList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载规格型号
			$.ajax({
				url:"basicData/getModel",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#modelList").html("");
						$("#modelList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载单位
			$.ajax({
				url:"basicData/getUnit",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#unitList").html("");
						$("#unitList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载默认仓库
			$.ajax({
				url:"basicData/getDefaultWarehouse",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#defaultWarehouseList").html("");
						$("#defaultWarehouseList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载供应商名称
			$.ajax({
				url:"basicData/getProviderName",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#providerNameList").html("");
						$("#providerNameList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载物料类型
			$.ajax({
				url:"basicData/getMaterialType",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#materialTypeList").html("");
						$("#materialTypeList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
			//加载编号
			$.ajax({
				url:"basicData/getColor",
				success:function(data){
					if(data.result){
						data = data.data;
						var html = "";
						for(var i=0;i<data.length;i++){
							html = html + "<option value='"+data[i]+"'/>";
						}
						$("#colorList").html("");
						$("#colorList").append(html);
					}else{
						layAlert(data.info);
					}
				}
			});
		    console.log("show");
		});
		//模态框隐藏后
		$('#addMaterialModal').on('hidden.bs.modal', function () {
			$(this).removeData("bs.modal");
		});
		//模态框点击添加按钮时
		$("#add").bind("click",function(){
			var materialName = $("#modalMaterialName").val();
			if(materialName==""){
				layAlert("请填写物料名称");
				return false;
			}
			var model = $("#modalModel").val();
			if(model==""){
				layAlert("请填写规格型号");
				return false;
			}
			var unit = $("#modalUnit").val();
			if(unit==""){
				layAlert("请填写单位");
				return false;
			}
			var unitPrice = $("#modalUnitPrice").val();
			if(unitPrice!="" && /^\d+(\.\d+)?$/.test(unitPrice)==false){//非负浮点数
				layer.msg("单价格式不符合规范");
				return false;
			}
			var status = $("#modalStatus").val();
			var defaultWarehouse = $("#modalDefaultWarehouse").val();
			if(defaultWarehouse==""){
				layAlert("请填写默认仓库");
				return false;
			}
			var providerName = $("#modalProviderName").val();
			if(providerName==""){
				layAlert("请填写供应商名称");
				return false;
			}
			var materialType = $("#modalMaterialType").val();
			if(materialType==""){
				layAlert("请填写物料类型");
				return false;
			}
			var procurementCycles = $("#modalProcurementCycles").val();
			if(procurementCycles!="" && /^\d+$/.test(procurementCycles)==false){//正整数
				layer.msg("采购周期只能是正整数");
				return false;
			}
			ajaxUtil.ajaxRuquest(
					'basicData/insertMaterial',
					page.insertParam(),
					function(data){
						table.ajax.reload( null, false);
						page.getMaterialCode();
						page.getMaterialName();
						page.getModel();
						page.getUnit();
						page.getDefaultWarehouse();
						page.getProviderName();
						page.getMaterialType();
						page.getColor();
						layAlert("添加成功!");
						$("#addMaterialModal").modal("hide");
					}
			);
		})
		//模态框编号改变时
		$("#modalColor").bind("change",function(){
			var color = $("#modalColor").val()
			ajaxUtil.ajaxRuquest(
					'basicData/getColorDescription',
					{'color':color},
					function(data){
						if(data!=""){
							//根据编号能查找到数据时
							$("#modalColorDescription").val(data);
						}else{
							//查不到数据时
							$("#modalColorDescription").val("");
						}
					}
			);
		});
		//改变物料代码时
		$("#materialCode").bind("change",function(){
			var materialCode = $("#materialCode").val();
			page.getMaterialDetail(materialCode);
		});
		//改变编号时
		$("#color").bind("change",function(){
			var color = $("#color").val();
			//连带获取描述
			page.getColorDescription(color);
		});
		//点击+物料名称按钮时
		$("#addMaterialNameBtn").bind("click",function(){
			$("#addMaterialNameTr").show("slow");
		});
		//点击添加物料名称按钮时
		$("#addMaterialNameBtn2").bind("click",function(){
			var addMaterialName = $("#addMaterialName").val();
			if(addMaterialName==""){
				layAlert("请输入要添加的物料名称");
				return false;
			}else{
				$("#addMaterialNameTr").hide("slow");
				$("#materialName").append("<option value="+addMaterialName+">"+addMaterialName+"</option>");
				$("#materialName option[value='"+addMaterialName+"']").attr("selected","selected");
			}
		});
		//点击+规格型号按钮时
		$("#addModelBtn").bind("click",function(){
			$("#addModelTr").show("slow");
		});
		//点击添加规格型号按钮时
		$("#addModelNameBtn").bind("click",function(){
			var addModelName = $("#addModelName").val();
			if(addModelName==""){
				layAlert("请输入要添加的规格型号");
				return false;
			}else{
				$("#addModelTr").hide("slow");
				$("#model").append("<option value="+addModelName+">"+addModelName+"</option>");
				$("#model option[value='"+addModelName+"']").attr("selected","selected");
			}
		});
		//点击+单位按钮时
		$("#addUnitBtn").bind("click",function(){
			$("#addUnitBTr").show("slow");
		});
		//点击添加单位按钮时
		$("#addUnitNameBtn").bind("click",function(){
			var addUnitName = $("#addUnitName").val();
			if(addUnitName==""){
				layAlert("请输入要添加的单位");
				return false;
			}else{
				$("#addUnitBTr").hide("slow");
				$("#unit").append("<option value="+addUnitName+">"+addUnitName+"</option>");
				$("#unit option[value='"+addUnitName+"']").attr("selected","selected");
			}
		});
		//点击+默认仓库按钮时
		$("#addDefaultWarehouseBtn").bind("click",function(){
			$("#addDefaultWarehouseTr").show("slow");
		});
		//点击添加默认仓库按钮时
		$("#addDefaultWarehouseNameBtn").bind("click",function(){
			var addDefaultWarehouseName = $("#addDefaultWarehouseName").val();
			if(addDefaultWarehouseName==""){
				layAlert("请输入要添加的默认仓库");
				return false;
			}else{
				$("#addDefaultWarehouseTr").hide("slow");
				$("#defaultWarehouse").append("<option value="+addDefaultWarehouseName+">"+addDefaultWarehouseName+"</option>");
				$("#defaultWarehouse option[value='"+addDefaultWarehouseName+"']").attr("selected","selected");
			}
		});
		//点击+供应商名称按钮时
		$("#addProviderNameBtn").bind("click",function(){
			$("#addProviderNameTr").show("slow");
		});
		//点击添加供应商名称按钮时
		$("#addProviderNameBtn2").bind("click",function(){
			var addProviderName = $("#addProviderName").val();
			if(addProviderName==""){
				layAlert("请输入要添加的供应商名称");
				return false;
			}else{
				$("#addProviderNameTr").hide("slow");
				$("#providerName").append("<option value="+addProviderName+">"+addProviderName+"</option>");
				$("#providerName option[value='"+addProviderName+"']").attr("selected","selected");
			}
		});
		//点击+物料类型按钮时
		$("#addMaterialTypeBtn").bind("click",function(){
			$("#addMaterialTypeTr").show("slow");
		});
		//点击添加物料类型按钮时
		$("#addMaterialTypeNameBtn").bind("click",function(){
			var addMaterialTypeName = $("#addMaterialTypeName").val();
			if(addMaterialTypeName==""){
				layAlert("请输入要添加的物料类型");
				return false;
			}else{
				$("#addMaterialTypeTr").hide("slow");
				$("#materialType").append("<option value="+addMaterialTypeName+">"+addMaterialTypeName+"</option>");
				$("#materialType option[value='"+addMaterialTypeName+"']").attr("selected","selected");
			}
		});
		//点击+编号按钮时
		$("#addColorBtn").bind("click",function(){
			$("#addColorTr").show("slow");
		});
		//点击添加编号按钮时
		$("#addColorNameBtn").bind("click",function(){
			var addColorName = $("#addColorName").val();
			var addColorDescriptionName = $("#addColorDescriptionName").val();
			if(addColorName==""){
				layAlert("请输入要添加的编号");
				return false;
			}else if(addColorDescriptionName==""){
				layAlert("请输入相应的描述");
				return false;
			}else{
				$("#addColorTr").hide("slow");
				$("#color").append("<option value="+addColorName+">"+addColorName+"</option>");
				$("#color option[value='"+addColorName+"']").attr("selected","selected");
				$("#colorDescription").val(addColorDescriptionName);
			}
		});
		//点击修改按钮时
		$("#updateBtn").bind("click",function(){
			var materialCode = $("#materialCode").val();
			if(materialCode=="请选择"){
				layAlert("请选择物料代码");
				return false;
			}
			var materialName = $("#materialName").val();
			if(materialName=="请选择"){
				layAlert("请选择物料名称");
				return false;
			}
			var model = $("#model").val();
			if(model=="请选择"){
				layAlert("请选择规格型号");
				return false;
			}
			var unit = $("#unit").val();
			if(unit=="请选择"){
				layAlert("请选择单位");
				return false;
			}
			var unitPrice = $("#unitPrice").val();
			if(unitPrice!="" && /^\d+(\.\d+)?$/.test(unitPrice)==false){//非负浮点数
				layer.msg("单价格式不符合规范");
				return false;
			}
			var status = $("#status").val();
			if(status=="请选择"){
				layAlert("请选择状态");
				return false;
			}
			var defaultWarehouse = $("#defaultWarehouse").val();
			if(defaultWarehouse=="请选择"){
				layAlert("请选择默认仓库");
				return false;
			}
			var providerName = $("#providerName").val();
			if(providerName=="请选择"){
				layAlert("请选择供应商名称");
				return false;
			}
			var materialType = $("#materialType").val();
			if(materialType=="请选择"){
				layAlert("请选择物料类型");
				return false;
			}
			var procurementCycles = $("#procurementCycles").val();
			if(procurementCycles!="" && /^\d+$/.test(procurementCycles)==false){
				layer.msg("采购周期只能是正整数");
				return false;
			}
			ajaxUtil.ajaxRuquest(
					'basicData/updateMaterial',
					page.updateParam(),
					function(data){
						table.ajax.reload( null, false);
						layAlert("修改成功");
					}
			);
		});
	},
	//获取物料代码
	getMaterialCode:function(){
		ajaxUtil.ajaxRuquest(
			'basicData/getMaterialCode',
			null,
			function(data){
				var html = "<option>请选择</option>";
				for(var i=0;i<data.length;i++){
					html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
				}
				$("#materialCode").html("");
				$("#materialCode").append(html);
			}
		);
	},
	//获取物料名称
	getMaterialName:function(){
		$.ajax({
			url:"basicData/getMaterialName",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#materialName").html("");
					$("#materialName").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取规格型号
	getModel:function(){
		$.ajax({
			url:"basicData/getModel",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#model").html("");
					$("#model").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取单位
	getUnit:function(){
		$.ajax({
			url:"basicData/getUnit",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#unit").html("");
					$("#unit").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取默认仓库
	getDefaultWarehouse:function(){
		$.ajax({
			url:"basicData/getDefaultWarehouse",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#defaultWarehouse").html("");
					$("#defaultWarehouse").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取供应商名称
	getProviderName:function(){
		$.ajax({
			url:"basicData/getProviderName",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#providerName").html("");
					$("#providerName").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取物料类型
	getMaterialType:function(){
		$.ajax({
			url:"basicData/getMaterialType",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#materialType").html("");
					$("#materialType").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//获取编号
	getColor:function(){
		$.ajax({
			url:"basicData/getColor",
			success:function(data){
				if(data.result){
					data = data.data;
					var html = "<option>请选择</option>";
					for(var i=0;i<data.length;i++){
						html = html + "<option value='"+data[i]+"'>"+data[i]+"</option>"
					}
					$("#color").html("");
					$("#color").append(html);
				}else{
					layAlert(data.info);
				}
			}
		});
	},
	//根据编号获取描述
	getColorDescription:function(color){
		ajaxUtil.ajaxRuquest(
				'basicData/getColorDescription',
				{'color':color},
				function(data){
					console.log(data);
					if(data!=""){
						//根据编号能查找到数据时
						$("#colorDescription").val(data);
					}else{
						//查不到数据时
						$("#colorDescription").val("");
					}
				}
		);
	},
	//获取物料详情
	getMaterialDetail:function(materialCode){
		ajaxUtil.ajaxRuquest(
				'basicData/getMaterialDetail',
				{'materialCode':materialCode},
				function(data){
					if(data!=""){
						//根据物料代码能查找到数据时
						$("#updateBtn").val(data[0].id);
						$("#materialCode").val(materialCode);
						$("#materialName").val(data[0].materialName);
						$("#model").val(data[0].model);
						$("#unit").val(data[0].unit);
						$("#unitPrice").val(data[0].unitPrice);
						$("#status").val(data[0].status);
						$("#defaultWarehouse").val(data[0].defaultWarehouse);
						$("#providerName").val(data[0].providerName);
						$("#materialType").val(data[0].materialType);
						$("#color").val(data[0].color);
						$("#colorDescription").val(data[0].colorDescription);
						$("#procurementCycles").val(data[0].procurementCycles);
						$("#creater").val(data[0].creater);
						$("#creationTime").val(data[0].creationTime);
						$("#lastModifier").val(data[0].lastModifier);
						$("#lastModifyTime").val(data[0].lastModifyTime);
					}else{
						//查不到数据时
						$("#updateBtn").val("");
						$("#materialName").val("");
						$("#model").val("");
						$("#unit").val("");
						$("#unitPrice").val("");
						$("#status").val("");
						$("#defaultWarehouse").val("");
						$("#providerName").val("");
						$("#materialType").val("");
						$("#color").val("");
						$("#colorDescription").val("");
						$("#procurementCycles").val(0);
						$("#creater").val("");
						$("#creationTime").val("");
						$("#lastModifier").val("");
						$("#lastModifyTime").val("");
					}
				}
		);
	},
	updateParam:function(){
		var id = $("#updateBtn").val();
		var materialCode = $("#materialCode").val();
		var materialName = $("#materialName").val();
		var model = $("#model").val();
		var unit = $("#unit").val();
		var unitPrice = $("#unitPrice").val();
		var status = $("#status").val();
		var defaultWarehouse = $("#defaultWarehouse").val();
		var providerName = $("#providerName").val();
		var materialType = $("#materialType").val();
		var color = $("#color").val();
		var colorDescription = $("#colorDescription").val();
		var procurementCycles = $("#procurementCycles").val();
		var sendData = {
				'id':id,
				'materialCode':materialCode,
				'materialName':materialName,
				'model':model,
				'unit':unit,
				'unitPrice':unitPrice,
				'status':status,
				'defaultWarehouse':defaultWarehouse,
				'providerName':providerName,
				'materialType':materialType,
				'color':color,
				'colorDescription':colorDescription,
				'procurementCycles':procurementCycles
		}
		return sendData;
	},
	insertParam:function(){
		var materialName = $("#modalMaterialName").val();
		var model = $("#modalModel").val();
		var unit = $("#modalUnit").val();
		var unitPrice = $("#modalUnitPrice").val();
		var status = $("#modalStatus").val();
		var defaultWarehouse = $("#modalDefaultWarehouse").val();
		var providerName = $("#modalProviderName").val();
		var materialType = $("#modalMaterialType").val();
		var color = $("#modalColor").val();
		var colorDescription = $("#modalColorDescription").val();
		var procurementCycles = $("#modalProcurementCycles").val();
		var sendData = {
				'materialName':materialName,
				'model':model,
				'unit':unit,
				'unitPrice':unitPrice,
				'status':status,
				'defaultWarehouse':defaultWarehouse,
				'providerName':providerName,
				'materialType':materialType,
				'color':color,
				'colorDescription':colorDescription,
				'procurementCycles':procurementCycles
		}
		return sendData;
	}
}
var tableData=[];//查询物料结果
//查询所有物料
var table;
function queryMaterial(){
	$('#queryMaterialTable tbody').unbind("click"); 
	$('#queryMaterialTable').dataTable().fnDestroy();
    table =  $('#queryMaterialTable').DataTable( {
   	  "ajax": {
             "url": "basicData/queryMaterial", 
             "dataSrc": "returnData", 
             "data": {},
          },
          "columns": [
				{"data":"materialName"},
				{"data":"materialType"},
				{"data":"status"},
				{"data":"color"},
				{"data":"colorDescription"}
			],
	       "processing": false, //打开数据加载时的等待效果
	       "serverSide": true,//打开后台分页
          "pagingType": "full_numbers",//设置分页控件的模式
          "searching": false,//屏蔽datatales的查询框
          "aLengthMenu":[20],//设置一页展示多少条记录
          "ordering":false,
          "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
          "responsive": true,//自适应
          "fixedHeader": true,//表头固定
          "oLanguage": {  //对表格国际化
             "sLengthMenu": "每页显示 _MENU_条",  
             "sZeroRecords": "没有找到符合条件的数据",  
             "sProcessing": "努力加载中",  
             "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",  
             "sInfoEmpty": "没有数据",  
             "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",  
             "oPaginate": {  
             "sFirst": "首页",  
             "sPrevious": "前一页",  
             "sNext": "后一页",  
             "sLast": "尾页"  
             }  
         }
    } );
	//行点击事件
	$('#queryMaterialTable tbody').on('click', 'tr', function () {
        var data = table.row(this).data();
        console.log(data);
        var materialCode = data.materialCode;
        console.log(materialCode);
        page.getMaterialDetail(materialCode);
    });
}